package com.situ.stmall.manger.service.impl;

import com.situ.stmall.manger.entity.Admin;
import com.situ.stmall.manger.mapper.AdminMapper;
import com.situ.stmall.manger.service.AdminService;
import com.situ.stmall.manger.util.MD5Util;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

@Service
//自动提供有参构造方法
@AllArgsConstructor
public class AdminServiceImpl implements AdminService {

    /**
     * spring的4.x版本后支持构造方法的自动注入
     * 提供一个有参的构造方法，不需要依赖于任何注解
     * 从4.x版本推荐使用构造方法注入
     */
    private AdminMapper adminMapper;

    @Override
    public Admin login(Admin admin) throws Exception {
        /**
         * 参数验证
         * 业务逻辑处理
         * 访问数据库
         */
        if(admin.getUsername()== null ){
            throw new Exception("用户名为空");
        }
        System.out.println(admin.getUsername());
        if(
        admin.getUsername().length()<3||
        admin.getUsername().length()>16){
            throw new Exception("用户名为空或长度不对");
        }
        if(admin.getPassword()== null ||
                admin.getPassword().length()<3||
                admin.getPassword().length()>16){
            throw new Exception("密码为空或长度不对");
        }

        //根据用户名查询
        Admin sAdmin = adminMapper.selectAdminByusername(admin.getUsername());
        if(sAdmin==null){
            throw new Exception("用户名不存在！！！");
        }
        //password是数据库中的密码
        String password = admin.getPassword();
        password = MD5Util.getMD5(password+sAdmin.getSalt());
        System.out.println(password);
        System.out.println("12313");

        if(!password.equals(sAdmin.getPassword())){
            throw new Exception("密码错误~~~");
        }
        //判断状态

        if(sAdmin.getStatus()==1){
            throw new Exception("用户被禁用");
        }

        return sAdmin;
    }

    @Override
    public Admin modifyPwd(Integer id, String oldPassword, String newPassword, String reptPassword) throws Exception {
        if(oldPassword==null ||
            oldPassword.length()<3 ||
            oldPassword.length()>16){
            throw new Exception("旧密码不正确");
        }
        if(newPassword==null ||
                newPassword.length()<3 ||
                newPassword.length()>16){
            throw new Exception("新密码不正确");
        }
        if(oldPassword.equals(newPassword)){
            throw new Exception("两次密码不一致");
        }

        /**
         * 输入的旧密码不正确
         */
        Admin admin = adminMapper.selectAdminById(id);
        String md5Pwd = MD5Util.getMD5(oldPassword+admin.getSalt());
        if(!md5Pwd.equals(admin.getPassword())){
            throw new Exception("旧密码不正确");
        }
        /**
         * 更新新密码
         */
        admin.setPassword(MD5Util.getMD5(newPassword+admin.getSalt()));
        /**
         * 更新管理员信息
         */
        adminMapper.update(admin);

        return admin;
    }

    @Override
    public int edit(Admin admin) throws Exception {
        //用户名的格式，密码，
        if(admin.getUsername()== null ||
                admin.getUsername().length()<3||
                admin.getUsername().length()>16){
            throw new Exception("用户名为空或长度不对");
        }
        if(admin.getPassword()== null ||
                admin.getPassword().length()<3||
                admin.getPassword().length()>16){
            throw new Exception("密码为空或长度不对");
        }

        Admin sAdmin = adminMapper.selectAdminByusername(admin.getUsername());
        if(sAdmin==null){
            throw new Exception("用户名不存在！！！");
        }

        return adminMapper.update(admin);
    }
}
